package com.ueboot.weishop.admin.controller.customer;

import com.alibaba.fastjson.JSON;
import com.ueboot.core.http.response.Response;
import com.ueboot.weishop.admin.controller.customer.vo.CustomerFavoritesFindReq;
import com.ueboot.weishop.admin.controller.customer.vo.CustomerFavoritesReq;
import com.ueboot.weishop.admin.controller.customer.vo.CustomerFavoritesResp;
import com.ueboot.weishop.entity.customer.CustomerFavoritesEntity;
import com.ueboot.weishop.entity.goods.GoodsInfoEntity;
import com.ueboot.weishop.service.customer.CustomerFavoritesService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Neel on 2017/9/18.
 */
@Slf4j
@RestController
@RequestMapping(value = "/customers/favorites")
public class CustomerFavoritesController {

    @Autowired
    private CustomerFavoritesService customerFavoritesService;

    @RequiresPermissions("customers.favorites:read")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public Response<Page<CustomerFavoritesResp>> page(@PageableDefault Pageable pageable, @RequestBody(required = false) CustomerFavoritesFindReq req) {
        log.info("/customers/favorites/page/");

        Page<CustomerFavoritesEntity> entities = customerFavoritesService.findBy(pageable);

        Page<CustomerFavoritesResp> body = entities.map(entity -> {
            GoodsInfoEntity goods = entity.getGoodsInfo();

            CustomerFavoritesResp resp = new CustomerFavoritesResp();
            BeanUtils.copyProperties(goods, resp);
            resp.setGift(GoodsInfoEntity.GIFT_1.equals(goods.getGift()) ? "是" : "否");
            resp.setMarketable(goods.getMarketable() ? "销售中" : "已下架");

            resp.setCustomerId(entity.getCustomer().getId());
            resp.setCategoryName(entity.getCustomer().getName());
            resp.setId(entity.getId());
            return resp;
        });

        log.info("response: {}", JSON.toJSON(body));
        return new Response<>(body);
    }


    @RequiresPermissions("customers.favorites:save")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Response<Void> save(@RequestBody CustomerFavoritesReq req) {
        log.info("/customers/favorites/save/");
        CustomerFavoritesEntity entity = null;
        if (req.getId() == null) {
            entity = new CustomerFavoritesEntity();
        } else {
            entity = customerFavoritesService.get(req.getId());
        }
        BeanUtils.copyProperties(req, entity);


        customerFavoritesService.save(entity);

        return new Response<>();
    }


    @RequiresPermissions("customers.favorites:delete")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Response<Void> delete(Long[] id) {
        log.info("/customers/favorites/delete/{}", id);
        customerFavoritesService.delete(id);
        return new Response<>();
    }

    @RequiresPermissions("customers.favorites:delete")
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public Response<Void> remove(Long id) {
        log.info("/customers/favorites/remove/{}", id);
        customerFavoritesService.delete(id);
        return new Response<>();
    }


    @RequiresPermissions("customers.favorites:read")
    @RequestMapping(value = "/find", method = RequestMethod.POST)
    public Response<Void> find(@RequestBody CustomerFavoritesFindReq req) {
        log.info("/customers/favorites/find/");

        return new Response<>();
    }


}
